package com.ellricky.baselib.ui

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ResUtil
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.RankAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.StaffRank
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import kotlinx.android.synthetic.main.base_fragment_rank_2.*
import java.util.*

const val TYPE_MONTH = 0
const val TYPE_YEAR = 1

class OverviewRankFragment : BaseFragment<Void, BasePresenter<Void>>() {

    private lateinit var adapter: RankAdapter
    private var currentState = TYPE_YEAR
    private var sortType = "1"

    override fun getContentLayout(): Int {
        return R.layout.base_fragment_rank_2
    }

    override fun initUIView(p0: View?) {
        val rankKeyArr = ResUtil.getStrArr(context, R.array.RankKey)
        label.setLabels(rankKeyArr.toList())

        adapter = RankAdapter(activity!!)
        rvRankNo.also {
            it.layoutManager = LinearLayoutManager(context)
            it.adapter = adapter
        }

        tvCurMonth.text = "当前年份:${getCurrentYear(System.currentTimeMillis())}"
        tvMonthTitle.setOnClickListener {
            when (currentState) {
                TYPE_MONTH -> {
                    currentState = TYPE_YEAR
                    tvMonthTitle.text = "按年显示"
                    tvCurMonth.text = "当前年份:${getCurrentYear(System.currentTimeMillis())}"
                    loadData()
                }
                TYPE_YEAR -> {
                    currentState = TYPE_MONTH
                    tvMonthTitle.text = "按月显示"
                    tvCurMonth.text = "当前月份:${getCurrentMonth(System.currentTimeMillis())}月"
                    loadData()
                }
            }
        }
        label.setOnLabelSelectChangeListener { _,
                                               _,
                                               _,
                                               position ->
            sortType = (position + 1).toString()
            loadData()
        }
        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "schoolSortList",
            "eid" to LocalCacheUtil.getUid(),
            "school_id" to "0",
            "sort_type" to sortType
        )
        paramMap["year_data"] = getCurrentYear(System.currentTimeMillis())
        if (currentState == TYPE_MONTH) {
            paramMap["month_data"] = getCurrentMonth(System.currentTimeMillis())
        }
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<StaffRank>(mContext) {
                override fun onSuccess(data: MutableList<StaffRank>?, msg: String?) {
                    adapter.refresh(data)
                }
            })
    }

    private fun getCurrentYear(time: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = time
        return cal[Calendar.YEAR].toString()
    }

    private fun getCurrentMonth(time: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = time
        val month = cal[Calendar.MONTH] + 1
        return month.toString()
    }
}